<%@ page language="java" session="true" errorPage="ElderhealthError.jsp"%>
<%@ page import="java.util.*,java.net.URL,java.net.URLConnection,java.net.URLEncoder" %>
<%@ page import="com.elderhealth.worker.HomecareWorker"%>
<%@ page import="com.elderhealth.client.Client"%>
<%@ page import="com.elderhealth.schedule.Schedule"%>
<%@ page import="com.elderhealth.schedule.Timeslot"%>
<%@ page import="com.elderhealth.schedule.Appointment"%>
<%
/*
 * Source:   GetWorkerSchedule.jsp
 * Purpose:  This page will pull the corrent Worker out of the already passed
 *           worker objects and display their attributes to the screen.
 */
%>

<html>
<head>
<link rel="stylesheet" type="text/css" href="elderhealth.css">
<!-- Scripting Portion -->
<script type="text/javascript" src="elderhealth.js"></script>
</head>
<body>

<%@ include file="ElderhealthHeader.jsp" %>

<%
/* Intialize variables */
String errorCode;
String errorMessage;
String redirectURL;

try
{
    /* Retrieve worker id from the passed parameters */
    int selectedWorkerID = Integer.parseInt(request.getParameter("workerID"));
    /* Retrieve workers and present them */
    HomecareWorker[] workerResults = (HomecareWorker[])session.getAttribute("results"); 
    /* Remove any old results being passed in the session as to avoid conflicts */
	session.removeAttribute("results");
%>

<table border=0 align="left" border=0 cellpadding=0 cellspacing=0 width="600">
    <tr class="contactHeaderText"><td height="18" align="left" valign="middle" style="PADDING-LEFT:10px">View Worker Schedule</td></tr>
    <tr><td class="layoutLine" valign="middle" align="left" width="302" height="1" colSpan="4"></td></tr>
    <tr class="contactBar1">
	  <td align="left" valign="middle" style="PADDING-LEFT:10px">

<%
    /* Check to make sure the workers from the search are still part of the session variable */
    if (workerResults == null || workerResults.length == 0)
    {
        errorCode = "1";
        errorMessage = "Error occurred in GetWorkerSchedule.jsp. No workers were passed to this page from the search page." +
                   "Probably a problem with the session. Please retry the search.";
        redirectURL = "ElderhealthError.jsp?errorCode=" + errorCode + "&errorMessage=" + errorMessage;
        response.sendRedirect(redirectURL);
    } 
    else
    {
        /* Initialize the worker variable */
        HomecareWorker selectedWorker = new HomecareWorker();
  
        /* Pull the correct worker from the list passed in*/
        for (int i = 0; i < workerResults.length; i ++)
        {
            if (workerResults[i].getWorkerId() == selectedWorkerID) 
	      {
	              selectedWorker = workerResults[i];
	      }
        }
  
        /* Make sure that the worker was found in the passed list */
        if (selectedWorker.getWorkerId() == -1) 
        {
            errorCode = "1";
            errorMessage = "Error occurred in GetWorkerSchedule.jsp. Selected worker was not in list of workers passed to this page." +
                   "Probably a problem with the session. Please retry the search.";
            redirectURL = "ElderhealthError.jsp?errorCode=" + errorCode + "&errorMessage=" + errorMessage;
            response.sendRedirect(redirectURL);
        }
        else
        {
            Schedule workerSchedule = selectedWorker.getSchedule(null, null);
            Appointment[] allAppointments = workerSchedule.getAppointments();
            Timeslot[]  allTimeSlots = workerSchedule.getTimeslots();

            /* Worker was found, display details */
%>
            Worker ID: <%=selectedWorker.getWorkerId()%><br>
	      First Name: <%=selectedWorker.getFirstName()%><br>
            Last Name: <%=selectedWorker.getLastName()%><br>
    
            <br><b>Appointments</b>
		<br><b>From&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
                        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; To&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
		&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
		&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with Clients</b><br>


<% 
            /* Check to make sure the workers appointment from the search are still part of the session variable */
            if (allAppointments == null || allAppointments.length == 0)
            {
%>
                There is no appointment <br>
<%
            } 
            else
            {

                for (int j = 0; j < allAppointments.length; j++)
                {
                    Appointment appt = allAppointments[j];
%>
                    <%=appt.getStartTime()%> &nbsp; &nbsp; &nbsp; &nbsp <%=appt.getEndTime()%>
                    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
					<a href="ViewClient.jsp?selectedClient=<%=appt.getClient().getClientId()%>"><%=appt.getClient().getClientName()%></a><br>
<%
                }
            }   
%>
            <br><b>Time slot</b><br>
<%
            if (allTimeSlots == null || allTimeSlots.length == 0)
            {
%>
                There is no time slot <br>
<%
            } 
            else
            {

                for (int k = 0; k < allTimeSlots.length; k++)
                {
                    Timeslot tslot = allTimeSlots[k];  
%>  
                    <%=tslot.startTime%> &nbsp; &nbsp; &nbsp; &nbsp; <%=tslot.endTime%><br>
<%
                }
            }
%>
	  <br><br>
        <input type="button" value="Go Back" onClick="history.back();"><br><br>
<%
        }
    }
} 

catch (Exception e) 
{
    errorCode = "1";
    errorMessage = "Error occurred in GetWorkerSchedule.jsp. Most likely error is that no worker id was submitted to this page. Error is:" +
                   e.toString();
    redirectURL = "ElderhealthError.jsp?errorCode=" + errorCode + "&errorMessage=" + errorMessage;
    response.sendRedirect(redirectURL);
}
%>
	<br>
    </td></tr>
    <tr><td class="layoutLine" valign="middle" align="left" width="302" height="1" colSpan="4"></td></tr>
</table>

</body>
</html>